package org.example.order.utils;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayConfig;
import com.alipay.api.DefaultAlipayClient;
import org.example.order.constant.AlipayConstant;
import org.example.order.properties.AlipayProperties;
import org.springframework.stereotype.Component;

/**
 * @author 小杰_cc
 * @version 1.0
 */
@Component
public class InitializationAlipay {
    private AlipayProperties alipayProperties;

    public InitializationAlipay(AlipayProperties alipayProperties){
        this.alipayProperties = alipayProperties;
    }

    public DefaultAlipayClient alipayClient() throws AlipayApiException {
        AlipayConfig alipayConfig = new AlipayConfig();

        // 基础配置（必须）
        alipayConfig.setAlipayPublicKey(alipayProperties.getPublicKey());
        alipayConfig.setAppId(alipayProperties.getAppId());
        alipayConfig.setPrivateKey(alipayProperties.getAppPrivateKey());
        alipayConfig.setServerUrl(AlipayConstant.GATEWAY_URL);
        alipayConfig.setSignType(AlipayConstant.SIGN_TYPE);
        alipayConfig.setFormat(AlipayConstant.FORMAT);
        alipayConfig.setCharset(AlipayConstant.CHARSET);

        // 高性能配置（可选）
        alipayConfig.setMaxIdleConnections(5);  //设置连接池中的最大可缓存的空闲连接数
        alipayConfig.setConnectTimeout(3000); //连接超时，单位：毫秒，默认3000
        alipayConfig.setReadTimeout(15000);   //读取超时，单位：毫秒，默认15000
        alipayConfig.setKeepAliveDuration(10000L);  //空闲连接存活时间，单位：毫秒，默认10000L


        return new DefaultAlipayClient(alipayConfig);
    }
}
